﻿using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.AutoMapper;
using Volo.Abp.Application;
using Volo.Abp.Domain;
using Volo.Abp.Uow;
using Volo.Abp.Authorization;
using Volo.Abp.Account.Web;
using Volo.Abp.Account;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.HttpApi;
using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.PermissionManagement;
using Volo.Abp;
using Volo.Abp.Swashbuckle;
using AbpApplication_Module.DBContext;
using AbpVnext_Module.Permission;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.TenantManagement;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;

namespace AbpApplication_Module
{
    [DependsOn(
        //配置基础设施模块
        typeof(AbpAspNetCoreMvcModule),
        typeof(AbpAutofacModule),
          typeof(AbpEntityFrameworkCoreModule),
        typeof(AbpEntityFrameworkCoreMySQLModule),
          typeof(AbpAutoMapperModule),
        typeof(AbpUnitOfWorkModule)
         , typeof(AbpAuthorizationModule)
        , typeof(AbpSwashbuckleModule)

        //配置权限应用模块
        , typeof(AbpPermissionManagementEntityFrameworkCoreModule)
        , typeof(AbpPermissionManagementApplicationModule)
        , typeof(AbpPermissionManagementHttpApiModule)

         //配置应用身份认证模块
         , typeof(AbpIdentityHttpApiModule)
        , typeof(AbpIdentityApplicationModule)
        , typeof(AbpIdentityEntityFrameworkCoreModule)
         , typeof(AbpPermissionManagementDomainIdentityModule)

          //配置应用账户模块
          , typeof(AbpAccountApplicationModule)
        , typeof(AbpAccountHttpApiModule)
        , typeof(AbpAccountWebModule)

          //配置多租户模块
          , typeof(AbpTenantManagementEntityFrameworkCoreModule)
        , typeof(AbpTenantManagementApplicationModule)
        , typeof(AbpTenantManagementHttpApiModule)

        //配置多租户切换模块
        , typeof(AbpAspNetCoreMultiTenancyModule)
        )]
    public class ProjectModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            // Add services to the container.
            IServiceCollection Services = context.Services;
            Services.AddControllers(option => {
                //全局取消默认token校验
                option.Filters.Add(new IgnoreAntiforgeryTokenAttribute());
            });
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            Services.AddEndpointsApiExplorer();
            Services.AddSwaggerGen();
            //设置  Cookie 身份认证
            Services.AddAuthentication().AddCookie();
            //设置权限
            Services.AddAuthorization();
            //注册商品图片上下文
            Services.AddAbpDbContext<ProductImageDbContext>();

            //集成mysql   
            Configure<AbpDbContextOptions>(option =>
            {
                option.UseMySQL();
            });

            //配置automap dto映射
            Configure<AbpAutoMapperOptions>(option => option.AddMaps<ProjectModule>());

            //配置AspNetCoreMvc模块
            Configure<AbpAspNetCoreMvcOptions>(option =>
            {
                //自动APi控制器
                //加载项目程序集  
                option.ConventionalControllers.Create(typeof(ProjectModule).Assembly, option =>
                {
                    //修改自动api控制器的根路径(abp)  //配置改造
                    option.RootPath = "projects";
                });
            });

            //services.AddSwaggerGen();
            Services.AddAbpSwaggerGen(option =>
            {
                //显示Abp自动api接口
                option.DocInclusionPredicate((x, y) => true);
            });

            ////配置自定义鉴权类
            Configure<AbpPermissionOptions>(option =>
            {
                //当注册多个权限值提供者时，PermissionGrantResult.Prohibited;
                //当其中有一个返回值为Prohibited，会直接返回403，授权没有通过
                //当返回值没有Prohibited,其中有一个返回值为Granted,则代表授权通过
                //当返回值全部为Undefined,,则代表授权没有通过
                option.ValueProviders.Add(typeof(CustomPermissionValueProvider));
            });

            //配置应用权限模块前缀
            AbpPermissionManagementDbProperties.DbTablePrefix = "YDT_";

            //配置应用身份认证模块前缀
            AbpIdentityDbProperties.DbTablePrefix = "YDT_";

            //配置应用租户模块
            AbpTenantManagementDbProperties.DbTablePrefix = "YDT_";

            //取消所有的权限校验
            context.Services.AddAlwaysAllowAuthorization();
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            IWebHostEnvironment webHostEnvironment = context.GetEnvironment();
            IApplicationBuilder applicationBuilder = context.GetApplicationBuilder();
            
            // Configure the HTTP request pipeline.
            if (webHostEnvironment.IsDevelopment())
            {
                applicationBuilder.UseSwagger();
                //app.UseSwaggerUI();
                applicationBuilder.UseAbpSwaggerUI(option => option.SwaggerEndpoint("/swagger/v1/swagger.json", "api API"));
            }
            //使用租户切换中间件
            applicationBuilder.UseMultiTenancy();
            //添加静态资源中间件
            applicationBuilder.UseStaticFiles();
            //使用路由
            applicationBuilder.UseRouting();
            //使用路由中间件
            applicationBuilder.UseHttpsRedirection();
            //身份验证中间件
            applicationBuilder.UseAuthentication();
            //授权
            applicationBuilder.UseAuthorization();

            //applicationBuilder.MapControllers();
            applicationBuilder.UseConfiguredEndpoints();
        }
    }
}
